﻿namespace Vibstudio.NetConfigurator.Engine.Validation
{
    #region Namespaces

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Linq;
    using System.Xml.Schema;

    using Vibstudio.NetConfigurator.Common;
    using Vibstudio.NetConfigurator.Globalization;
    using Vibstudio.NetConfigurator.Model;

    #endregion

    public class ConfigurationFileValidator : IValidation<ConfigurationFile>
    {
        #region Public Methods and Operators

        public List<ResultValidation<ConfigurationFile>> Validate()
        {
            var result = new List<ResultValidation<ConfigurationFile>>();

            try
            {
                string applicationsFilePath = ConfigurationHelper.ApplicationsFilePath;
                string configurationsFilePath = ConfigurationHelper.ConfigurationsFilePath;

                try
                {
                    string validationMessage = string.Empty;
                    bool isValid = true;

                    XDocument applicationsFile = XDocument.Load(applicationsFilePath);
                    applicationsFile.Validate(ConfigurationHelper.GetApplicationsSchemaSet(),
                                              (o, e) =>
                                                  {
                                                      validationMessage = e.Message;
                                                      isValid = false;
                                                  });

                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ApplicationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       isValid,
                                                                       validationMessage));
                }
                catch (FileNotFoundException)
                {
                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ApplicationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       false,
                                                                       Labeler.Get.FileNotFound(applicationsFilePath).ToString()));
                }
                catch (Exception exception)
                {
                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ApplicationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       false,
                                                                       exception.Message));
                }

                try
                {
                    string validationMessage = string.Empty;
                    bool isValid = true;

                    XDocument configurationsFile = XDocument.Load(configurationsFilePath);
                    configurationsFile.Validate(ConfigurationHelper.GetConfigurationsSchemaSet(),
                                                (o, e) =>
                                                {
                                                    validationMessage = e.Message;
                                                    isValid = false;
                                                });

                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ConfigurationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       isValid,
                                                                       validationMessage));
                }
                catch (FileNotFoundException)
                {
                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ApplicationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       false,
                                                                       Labeler.Get.FileNotFound(configurationsFilePath).ToString()));
                }
                catch (Exception exception)
                {
                    result.Add(new ResultValidation<ConfigurationFile>(ConfigurationHelper.ConfigurationsFileName,
                                                                       ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                       false,
                                                                       exception.Message));
                }
            }
            catch (Exception exception)
            {
                return new List<ResultValidation<ConfigurationFile>>
                           {
                               new InvalidResultValidation<ConfigurationFile>(ConfigurationFile.Create(ConfigurationHelper.ExecutablePath),
                                                                              exception.Message)
                           };
            }

            return result;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
